//
//  RootViewController.m
//  JackRadar
//
//  Created by Pei on 15/11/9.
//  Copyright © 2015年 Xiaobo Pei. All rights reserved.
//

#import "RootViewController.h"
#import "TabBarItem.h"
#import "TabBarButton.h"



#define kTabOneWidth kDeviceWidth/4.0f

#define kMaxTabItems 4
#define MAKTAG(a) ((a)+1)
#define GETTAG(a) ((a)-1)

#define kTabWidth kDeviceWidth



@interface LSLTabPrivate : NSObject
{
    NSString *_normal;
    NSString *_selected;
    UIViewController *_controller;
}
@property (nonatomic,retain) NSString *normalImage;
@property (nonatomic,retain) NSString *selectedImage;
@property (nonatomic,retain) UIViewController *controller;

@end

@implementation LSLTabPrivate

@synthesize normalImage = _normal, selectedImage = _selected,controller = _controller;

@end

@interface RootViewController ()

@end

@interface RootViewController ()
{
    NSString *currentString;
    UIView *guidingView;
    UIImageView *imageView;
    UIButton *button;
}
@end

@implementation RootViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.tabBar.hidden = YES;
    _controllerArr = [[NSMutableArray alloc]init];
    
    self.view.backgroundColor = kWhiteColor;//[JackTool colorWithHexString:@"#F6F6F6"];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(tabBarHidden) name:Hide_TabBar_Notification object:nil];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(tabBarNotHidden) name:Not_Hide_TabBar_Notification object:nil];
}

- (void)setViewControllers:(NSArray *)viewControllers {
    
    NSMutableArray *arr = [NSMutableArray array];
    for (NSDictionary *dict in viewControllers) {
        
        UIViewController *c = [dict objectForKey:kViewController];
        
        LSLTabPrivate *tp = [[LSLTabPrivate alloc]init];
        tp.normalImage = [dict objectForKey:kNormalImage];
        tp.selectedImage = [dict objectForKey:kSelectedImage];
        tp.controller = c;
        [_controllerArr addObject:tp];
        //_controllerArr 我们自己的数据管理
        [arr addObject:c];
    }
    [super setViewControllers:arr];
    [self createTabBar];
}




- (void)createTabBar {
    
    CGFloat y = kDeviceHeight - kTabHeight;
    CGRect rect = CGRectMake(0, y, kTabWidth, kTabHeight);
    bgView = [[UIImageView alloc]initWithFrame:rect];
    bgView.userInteractionEnabled = YES;
    
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = kRGBA(200, 199, 213, 0.5f);
    lineView.frame = CGRectMake(0.0f, 0.0f, y, 0.5f);
    [bgView addSubview:lineView];
    
    //[bgView setImage:[JackTool imageWithColor:[UIColor whiteColor]]];
    bgView.backgroundColor = kWhiteColor;
    [self.view addSubview:bgView];
    
    [self createItems];
    
    
    
    //    NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
    //    NSString *value = [settings objectForKey:@"is_first"];
    //    if ([value isEqualToString:@"1"]) {
    //        [self showGuidingView];
    //
    //        [settings setObject:@"2" forKey:@"is_first"];
    //        [settings synchronize];
    //    }
}
- (void)tabBarHidden {
    bgView.hidden = YES;
}
- (void)tabBarNotHidden {
    bgView.hidden = NO;
}



- (void)createItems
{
    //话题，测试，发现，我的
    //NSArray *array = [NSArray arrayWithObjects:kStr(@"营养方案"),kStr(@"健康建议"),kStr(@""), kStr(@"我的用药"),kStr(@"个人中心"),nil];
    
    int numOfTabs = MIN((int)_controllerArr.count, kMaxTabItems);
    
    for (int i=0; i<numOfTabs; i++) {
        
        float x = i*kTabOneWidth;
        CGRect rect;
        TabBarItem *itemView;
        LSLTabPrivate *tp = [_controllerArr objectAtIndex:i];
        
        rect = CGRectMake(x, 0.0, kTabOneWidth, 50);
        itemView = [[TabBarItem alloc] initWithFrame:rect];
        [itemView setNormalImage:[UIImage imageNamed:tp.normalImage]];
        [itemView setSelectedImage:[UIImage imageNamed:tp.selectedImage]];
        [itemView setTitle:[self.titleArray objectAtIndex:i] forState:UIControlStateNormal];
        itemView.titleLabel.font = kAppFont(20.0f/2);
        [itemView setTitleColor:kGreenColor forState:UIControlStateNormal];
        [itemView setTitleColor:kBlueColor forState:UIControlStateHighlighted];
        [itemView setTitleColor:kBlueColor forState:UIControlStateSelected];
        
        [itemView addTarget:self action:@selector(itemClick:) forControlEvents:UIControlEventTouchUpInside];
        [itemView setTag:MAKTAG(i)];
        
        //设置默认显示哪一项
        [self clickTabWithIndex:1];
        
        [bgView addSubview:itemView];
    }
}

- (void)clickTabWithIndex:(int) index
{
    [self tabBarNotHidden];
    NSArray *allTabs = [bgView subviews];
    for (UIView *v in allTabs)
    {
        if ([v isKindOfClass:[TabBarButton class]])
        {
            TabBarButton *ti = (TabBarButton *)v;
            if (ti.tag == index)
            {
                [self itemClick:ti];
                break;
            }
        }
    }
}

//tabBar按键控制
- (void)itemClick:(TabBarButton *)sender
{
    int index = (int)GETTAG(sender.tag);
    NSArray *allTabs = [bgView subviews];
    for (UIView *v in allTabs)
    {
        
        if ([v isKindOfClass:[TabBarButton class]])
        {
            
            TabBarButton *ti = (TabBarButton *)v;
            [ti setSelected:NO];
            
            LSLTabPrivate *tp = [_controllerArr objectAtIndex:GETTAG(ti.tag)];
            //            if (ti.tag == 3)
            //            {
            //                [ti setBackgroundImage:[UIImage imageNamed:tp.normalImage] forState:UIControlStateNormal];//所有按钮设置正常状态
            //                [ti setNormalImage:nil];//所有按钮设置正常状态
            //            }
            //            else
            //            {
            [ti setNormalImage:[UIImage imageNamed:tp.normalImage]];//所有按钮设置正常状态
            //            }
            
            [ti setTitleColor:kGreenColor forState:UIControlStateNormal];
            [ti setTitleColor:kBlueColor forState:UIControlStateHighlighted];
            [ti setTitleColor:kBlueColor forState:UIControlStateSelected];
        }
        
    }
    
    [sender setTitleColor:kGreenColor forState:UIControlStateNormal];
    [sender setTitleColor:kBlueColor forState:UIControlStateHighlighted];
    [sender setTitleColor:kBlueColor forState:UIControlStateSelected];
    sender.selected = YES;
    
    
    self.selectedIndex = index;
    [self tabBarNotHidden];
    
    if (index == 0)
    {
        
    }
    else if (index == 1)
    {
        
    }
    else if (index == 3)
    {
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
